<template>
  <div class="flex flex-col h-full">
    <a-card>
      <div class="flex justify-between items-center">
        <a-button type="primary" class="mr-4" @click="router.go(-1)"> 返回 </a-button>
        <a-descriptions class="mt-3" :column="5">
          <a-descriptions-item label="单号"> {{ formState.lsh || '-' }}</a-descriptions-item>
          <a-descriptions-item label="盘点仓库">{{ formState.ckmc || '-' }}</a-descriptions-item>
          <a-descriptions-item label="">{{ formState.zeroFlag === 1 ? '包含零库存项目' : '不包含零库存项目' || '-' }}</a-descriptions-item>
          <a-descriptions-item label="制单人"> {{ formState.createUser || '-' }}</a-descriptions-item>
          <a-descriptions-item label="制单日期">{{ formState.createTime || '-' }}</a-descriptions-item>
        </a-descriptions>
      </div>
    </a-card>
    <a-card class="mt-2 flex-1">
      <div class="flex justify-between mb-2">
        <div class="flex items-center">
          <BaseInput
            v-model:value="tcCode"
            auto-mark
            :auto-focus="autoFocus"
            placeholder="请扫码"
            maxlength="20"
            class="w-64 mr-4"
            @change="onChangeCode"
            @keydown.enter="onSaveData"
          />
          <span>合并药品</span>
          <a-switch v-model:checked="merge" class="ml-1 mr-2" @change="getDetailData()" />
          <DrugInfo :value="{ mc: drugInfo.ypmc, gg: drugInfo.gg, cj: drugInfo.cj, ph: drugInfo.ph, yxq: drugInfo.expirationTime }" />
        </div>
        <a-space>
          <a-button @click="onPrint"> 盘点差异表 </a-button>
          <a-button v-if="formState.status === 1" type="primary" @click="onClickType(2)"> 盘点完毕 </a-button>
          <a-button v-else type="primary" danger @click="onClickQuash(1)"> 撤销完毕 </a-button>
        </a-space>
      </div>
      <BaseTable :data-source="tableData" :column-code="merge ? '000983' : '000762'" :loading="loading" fill :extra-height="64" :pagination="pagination">
        <template #bodyCell="{ column, index, record }">
          <template v-if="column.dataIndex === 'xh'">
            {{ index + 1 }}
          </template>
          <template v-if="column.dataIndex === 'mc'">
            <MedicineInfo :value="{ mc: record.mc, gg: record.gg, cj: record.cj }" />
          </template>
          <template v-if="column.dataIndex === 'diffSl'">
            <div v-if="record.diffSl === 0">{{ record.diffSl }}</div>
            <div v-else-if="record.diffSl > 0" class="text-red-500">{{ record.diffSl }}</div>
            <div v-else-if="record.diffSl < 0" class="text-blue-700">{{ record.diffSl }}</div>
          </template>
          <template v-if="column.dataIndex === 'action'">
            <a-button type="link" size="small" @click="onOpenView(record)">查看追溯码</a-button>
          </template>
        </template>
      </BaseTable>
    </a-card>
    <ViewTrace v-model:open="viewTraceOpen" :row-data="{ ...rowData, formSn: formState.lsh }" />
  </div>
</template>

<script setup>
import { ref, watchEffect } from 'vue'
// import { message } from 'ant-design-vue'
import { useRoute, useRouter } from 'vue-router'
import { message } from 'ant-design-vue'
import BaseTable from '@/components/BaseTable'
import { post } from '@/utils/request'
import ViewTrace from '../components/ViewTrace.vue'
import DrugInfo from '../components/DrugInfo.vue'

const formState = ref({})
const route = useRoute()
const router = useRouter()
const zjm = ref('')
const merge = ref(false)
const viewTraceOpen = ref(false)
const tcCode = ref('')
const rowData = ref({})
const drugInfo = ref({})
const autoFocus = ref(false)

const onSaveData = async () => {
  if (!tcCode.value) return
  autoFocus.value = false
  const { code, msg, data } = await post('/drugStorage/psiform/doSaveTcCodeDrug', {
    formSn: route.query?.lsh,
    operationMode: 20,
    tcCode: tcCode.value,
    drugStoreId: formState.value.ckid
  })
  autoFocus.value = true
  tcCode.value = ''
  if (code !== 0) return message.error(msg || '操作失败')
  drugInfo.value = data || {}
  getData()
}

const getMainData = async () => {
  //获取盘点单总表列表
  const { data, code } = await post('/drugStorage/psiform/getInventoryHeadList', {
    dhmc: route.query?.lsh
  })
  if (code === 0) {
    formState.value = data[0]
  }
}

//盘点结束
const onClickType = async (status) => {
  if (tableData.value.every((item) => item.checkSl !== '')) {
    const { msg, code } = await post('/drugStorage/psiform/doUpdateCheckStatus', { ...formState.value, status, tcCode: '1' })
    if (code !== 0) return message.error(msg)
    message.success(msg)
    getMainData()
  } else {
    message.error('盘点数不可为空')
  }
}

//取消盘点
const onClickQuash = async (status) => {
  const { msg, code } = await post('/drugStorage/psiform/doRollbackCheck', { ...formState.value, status })
  if (code !== 0) return message.error(msg)
  getMainData()
  message.success(msg)
}

const { loading, tableData, getData, pagination } = useTable({
  url: '/drugStorage/psiform/getInventoryDetailList',
  immediate: false,
  formatParams: () => {
    return {
      lsh: route.query.lsh,
      zjm: zjm.value,
      merge: merge.value
    }
  }
  // onSuccess: () => {
  //   tableData.value.forEach((i) => {
  //     i.checkSl = i.storeSl
  //   })
  // }
})
// const getDetailData = async () => {
//   //盘点明细
//   loading.value = true
//   const { data, code } = await post('/drugStorage/psiform/getInventoryDetailList', {
//     lsh: route.query.lsh,
//     zjm: zjm.value,
//     pageSize: 999,
//     pageNum: 1
//   })
//   loading.value = false
//   if (code === 0) {
//     // data.forEach((i) => {
//     //   i.checkSl = i.storeSl
//     // })
//     dataSource.value = data
//   }
// }

// 盘点差异表打印
const onPrint = async () => {
  const { code, msg, data } = await post('/drugStorage/psiform/printKcStoreCheckCyList', { lsh: formState.value.lsh })
  if (code !== 0) return message.error(msg)
  window.openExe?.rpt(data) //调用winform打印
}
const onOpenView = (row) => {
  rowData.value = row
  viewTraceOpen.value = true
}
const onChangeCode = () => {
  drugInfo.value = {}
}

watchEffect(() => {
  if (route.query?.lsh) {
    getMainData()
    getData()
  }
})
</script>
<style lang="less" scoped>
:deep(.ant-card-head) {
  padding-left: 8px;
}
:deep(.ant-table-row-expand-icon) {
  display: none;
}
</style>
